package school

import (
	"data/config"
	"data/view/auth"
	"data/view/auth/auth_const"
	"data/view/user"
	"public/message"
	"public/mysqldb"
	"public/tools"

	"github.com/ant0ine/go-json-rest/rest"
)

/*
	新增或编辑学校信息
*/
func Action(w rest.ResponseWriter, r *rest.Request) {
	var req ActionParam
	tools.GetRequestJsonObj(r, &req)

	//参数检测
	if !tools.CheckParam(req.Access_token, req.Name) || req.Action == 0 {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}
	//验证token，并获取用户名
	username, _, bfind := user.GetUserFromToken(req.Access_token)
	if !bfind {
		w.WriteJson(message.GetErrorMsg(message.TokenFailure))
		return
	}
	u, bb := user.GetUserDetail(username)
	if !bb {
		w.WriteJson(message.GetErrorMsg(message.UserNotExisted))
		return
	}
	//权限检验
	b := auth.CheckRoleAuth(u.Business_role, auth_const.M_1, auth_const.L_A, auth_const.L_C)
	if !b {
		w.WriteJson(message.GetErrorMsg(message.UserMenuNotExisted))
		return
	}
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var err error

	if req.Action == 1 { //新增
		//参数检测
		if !tools.CheckParam(req.School_type) || req.Lat == 0 || req.Lon == 0 || req.Clock_range == 0 {
			w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
			return
		}
		var tmp School_info_tbl
		orm.Where("name = ? and province = ? and city = ? and region = ?", req.Name, req.Province, req.City, req.Region).Find(&tmp)

		if tmp.Id > 0 {
			w.WriteJson(message.GetErrorMsg(message.ExistedError))
			return
		}
		req.Last_operator = username
		err = orm.Table("school_info_tbl").Create(&req).Error
	} else if req.Action == 2 { //修改

		var t, tp School_info_tbl
		orm.Where("name = ? and province = ? and city = ? and region = ?", req.Name_new, req.Province_new, req.City_new, req.Region_new).Find(&tp)
		if tp.Id > 0 {
			w.WriteJson(message.GetErrorMsg(message.ExistedError))
			return
		}
		t.Attach = req.Attach
		t.Clock_range = req.Clock_range
		t.File = req.File
		t.Last_operator = username
		t.Lat = req.Lat
		t.Lon = req.Lon
		t.Name = req.Name_new
		t.Province = req.Province_new
		t.City = req.City_new
		t.Region = req.Region_new
		t.School_id = req.School_id
		t.School_type = req.School_type
		err = orm.Table("school_info_tbl").Where("name = ? and province = ? and city = ? and region = ?", req.Name, req.Province, req.City, req.Region).
			Updates(t).Error
	} else {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}
	if err != nil {
		w.WriteJson(message.GetErrorMsg(message.UnknownError))
		return
	}
	msg := message.GetSuccessMsg()
	w.WriteJson(msg)
}

//获取全部学校信息
func GetAll(w rest.ResponseWriter, r *rest.Request) {
	var req ActionParam
	tools.GetRequestJsonObj(r, &req)

	//参数检测
	if !tools.CheckParam(req.Access_token) {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}
	//验证token，并获取用户名
	username, _, bfind := user.GetUserFromToken(req.Access_token)
	if !bfind {
		w.WriteJson(message.GetErrorMsg(message.TokenFailure))
		return
	}
	u, bb := user.GetUserDetail(username)
	if !bb {
		w.WriteJson(message.GetErrorMsg(message.UserNotExisted))
		return
	}
	//权限检验
	b := auth.CheckRoleAuth(u.Business_role, auth_const.M_1, auth_const.L_A, auth_const.L_B, auth_const.L_C)
	if !b {
		w.WriteJson(message.GetErrorMsg(message.UserMenuNotExisted))
		return
	}
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var schools []School_info_tbl

	var sql = orm.Where("1 = 1")
	if len(req.Name) > 0 {
		sql = sql.Where("name like ?", "%"+req.Name+"%")
	}
	if len(req.Province) > 0 {
		sql = sql.Where("province = ?", req.Province)
		if len(req.City) > 0 {
			sql = sql.Where("province = ? and city = ?", req.Province, req.City)
			if len(req.Region) > 0 {
				sql = sql.Where("province = ? and city = ? and region = ?", req.Province, req.City, req.Region)
			}
		}
	}
	sql.Find(&schools)
	msg := message.GetSuccessMsg()
	msg.Data = schools
	w.WriteJson(msg)
}
